package com.zhuss.order.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.SearchOrderBo;
import com.zhuss.bean.vo.order.order.ListOrderVo;
import com.zhuss.common.PageVo;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.OceanException;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.entity.Order;


/**
 * @description: 订单读
 * @author: zhushanshuo
 * @create: Wed Mar 24 22:32:50 CST 2021
 **/
@Component("orderRead")
public class OrderRead {
	
	@Autowired
	private OrderDao orderDao;
	
	
	/**
	 * 订单
	 * @return
	 */
	public Object listOrder(List<Long> orderIdList) {
		List<Order> result = orderDao.listById(orderIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询订单
	 * @param 
	 * @return
	 */
	public List<Order> list(String name) {
		List<Order> result = orderDao.listByName(name);
		return result;
	}
	
	public List<Order> listNotId(String name, Long orderId) {
		List<Order> result = new ArrayList<Order>();
		List<Order> list = list(name);
		for (Order c : list) {
			if (!c.getOrderId().equals(orderId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 订单 
	 * @return
	 */
	public Object listOrderId() {
		List<Order> orderList = orderDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(orderList, result);
		return result;
	}
	
	private void transferListId(List<Order> orderList, List<Long> result) {
		for (Order item : orderList) {
			result.add(item.getOrderId());
		}
	}


	/**
	 * 订单列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchOrderBo bo) {
		PageVo<ListOrderVo> result = new PageVo<ListOrderVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(orderDao.list(bo));
        result.setTotal(orderDao.countList(bo));
		return result;
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 * @return
	 * @throws OceanException
	 */
	public Object detail(Long orderId) throws OceanException {
		Object result = orderDao.detail(orderId);
		if (result == null) {
			throw new OceanException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
